﻿using EMoney.IStock.Client.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.Framework
{
    /// <summary>
    /// 发送短信或邮件
    /// </summary>
    public class SendHelper
    {
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="content"></param>
        public static bool SendSms(string mobile, string content)
        {
            bool ret = false;

            try
            {
                string url = string.Concat(ConfigHelper.WebAPIUrl, "/Mobile/api/Sms.SendSms_New?appId=", 
                    ConfigHelper.UserAppId, "&Mobile=", mobile, "&Content=", content, "&DeptID=15&Description=222000000|114");

                url = WebApiHelper.CreateUrl(url, ConfigHelper.UserWebApiKey);

                NetHelper net = new NetHelper();

                string result = net.GetClientBySocket(url);

                if (!string.IsNullOrEmpty(result))
                {
                    WebApiJson webApiJson = JsonHelper.ToObject<WebApiJson>(result);

                    if (webApiJson != null && webApiJson.RetCode == "0")
                    {
                        ret = true;
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(ex, string.Concat("发送短信失败.", mobile, ".", content));
            }

            return ret;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="email"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public static bool SendEmail(string email, string subject, string body)
        {
            try
            {
                System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("istock@emoney.cn", "益盟爱炒股");
                System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(email, "爱炒股用户");
                
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                mail.From = from;
                mail.To.Add(to);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                
                client.Host = "172.28.1.1";
                
                client.Credentials = new System.Net.NetworkCredential("istock@emoney.cn", "zhuce1qaz=[;.");
                
                client.Send(mail);

                return true;
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(ex, string.Concat("发送邮件失败.", email, ".", subject));

                return false;
            }
        }
    }
}
